////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#ifndef __WN_CONCURRENCY_RECURSIVE_SPIN_MUTEX_H__
#define __WN_CONCURRENCY_RECURSIVE_SPIN_MUTEX_H__

#include "WNCore/inc/WNTypes.h"

#ifdef _WN_WINDOWS
    #include <windows.h>
#elif defined _WN_LINUX
    #include <unistd.h>
#endif

namespace WNConcurrency {
    class WNRecursiveSpinMutex {
    public:
        WN_FORCE_INLINE WNRecursiveSpinMutex();

        WN_FORCE_INLINE WN_VOID Lock();
        WN_FORCE_INLINE WN_BOOL TryLock();
        WN_FORCE_INLINE WN_VOID Unlock();

    private:
        WNRecursiveSpinMutex(const WNRecursiveSpinMutex&);
        WNRecursiveSpinMutex& operator = (const WNRecursiveSpinMutex&);

    private:
        #ifdef _WN_WINDOWS
            DWORD mOwningThread;
        #elif defined _WN_LINUX
            pid_t mOwningThread;
        #endif

        WN_UINT32 mRecursionCount;
        WN_ATOM_T mSpinFlag;
    };
}

#include "WNConcurrency/inc/WNRecursiveSpinMutex.inl"

#endif // __WN_CONCURRENCY_RECURSIVE_SPIN_MUTEX_H__